package com.dagene.weixin.entity;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name = "chat_msg")
public class ChatMsg {

	long msgid;
	String openid;
	short origin; // 消息起源，0：用户；1：客服
	String msgtype; // 消息类型
	String fileurl;// 图片等文件路径
	Date time;	// 操作时间
	String worker;// 客服名
	int matchnumber; // 聊天匹配编号

	String message;

	// 临时属性
	String timeStr;// 操作时间字符串
	WeixinUserInfo userinfo; // 微信用户信息
	String wokerName; // 客服名字
	String userName; // 用户名字
	String msgDirection; // 信息方向
	String contain; // 消息内容
	String dateStr; // 时间

	public ChatMsg() {

	}

	public ChatMsg(String openid, String msgtype, String message,
			String fileurl, Date time, String worker) {
		this.openid = openid;
		this.msgtype = msgtype;
		this.message = message;
		this.fileurl = fileurl;
		this.time = time;
		this.worker = worker;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	public long getMsgid() {
		return msgid;
	}

	public void setMsgid(long msgid) {
		this.msgid = msgid;
	}

	public String getOpenid() {
		return openid;
	}

	public void setOpenid(String openid) {
		this.openid = openid;
	}

	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}

	public String getWorker() {
		return worker;
	}

	public void setWorker(String worker) {
		this.worker = worker;
	}

	@Transient
	public String getTimeStr() {
		return timeStr;
	}

	public void setTimeStr(String timeStr) {
		this.timeStr = timeStr;
	}

	public String getMsgtype() {
		return msgtype;
	}

	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}

	public String getFileurl() {
		return fileurl;
	}

	public void setFileurl(String fileurl) {
		this.fileurl = fileurl;
	}

	@Transient
	public WeixinUserInfo getUserinfo() {
		return userinfo;
	}

	public void setUserinfo(WeixinUserInfo userinfo) {
		this.userinfo = userinfo;
	}

	public short getOrigin() {
		return origin;
	}

	public void setOrigin(short origin) {
		this.origin = origin;
	}

	public int getMatchnumber() {
		return matchnumber;
	}

	public void setMatchnumber(int matchnumber) {
		this.matchnumber = matchnumber;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Transient
	public String getWokerName() {
		return wokerName;
	}

	public void setWokerName(String wokerName) {
		this.wokerName = wokerName;
	}

	@Transient
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Transient
	public String getMsgDirection() {
		// 0:用户发给客服，1：客服发给用户
		if (origin == 0) {
			return "←";
		} else {
			return "→";
		}
	}

	public void setMsgDirection(String msgDirection) {
		this.msgDirection = msgDirection;
	}

	@Transient
	public String getContain() {
		if (msgtype.equals("text") || msgtype.equals("event")) {
			return getMessage();
		} else {
			return getFileurl();
		}
	}

	public void setContain(String contain) {
		this.contain = contain;
	}

	@Transient
	public String getDateStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return sdf.format(getTime());
	}

	public void setDateStr(String dateStr) {
		this.dateStr = dateStr;
	}

}
